package com.gupao.study.prototype;

import lombok.Data;
import net.sf.json.JSONObject;

import java.io.Serializable;
import java.util.List;

@Data
public class Prototype implements Cloneable, Serializable {

    private String name;
    private int age;
    private List<String> hobby;

    /**
     * 使用json进行深克隆
     * @return
     */
    public Prototype jsonClone(){
        JSONObject jsonObject= JSONObject.fromObject(this);
        return (Prototype)JSONObject.toBean(jsonObject,Prototype.class);
    }

    /**
     * 使用父类的方法进行浅克隆
     * @return
     */
    @Override
    public Prototype clone(){
        try {
            Object obj = super.clone();
            return (Prototype)obj;
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return null;
    }
}
